package com.nuctech.hardwaredriver.serialport_library.core;

import com.nuctech.hardwaredriver.serialport_library.core.codec.SerialPortHandler;
import com.nuctech.hardwaredriver.serialport_library.core.codec.SerialPortProtocolCodecFactory;

/**
 * Created by 心魂free on 2019/10/30.
 * <p>
 * 串口配置类，采用构造这模式，目前暂不使用停止位，数据位，奇偶校验
 */

public class SerialPortConfig {

    private String serialPortNumber;        //串口号
    private int baudRate;                   //波特率
    private int stopBits;                   //停止位
    private int dataBits;                   //数据位
    private int parity;                     //校验位
    private int flowCon;                    //串口流控
    private int flags;                      //标志位
    private int timeOut;                    //超时
    private SerialPortProtocolCodecFactory codecFactory;    //编解码工厂
    private SerialPortHandler serialPortHandler;    //接收返回数据

    public String getSerialPortNumber() {
        return serialPortNumber;
    }

    public int getBaudRate() {
        return baudRate;
    }

    public int getStopBits() {
        return stopBits;
    }

    public int getDataBits() {
        return dataBits;
    }

    public int getParity() {
        return parity;
    }

    public int getFlowCon() {
        return flowCon;
    }

    public int getFlags() {
        return flags;
    }

    public int getTimeOut() {
        return timeOut;
    }

    public SerialPortProtocolCodecFactory getCodecFactory() {
        return codecFactory;
    }

    public SerialPortHandler getSerialPortHandler() {
        return serialPortHandler;
    }

    public static class Builder {

        private String serialPortNumber = "/dev/ttyS1";
        private int baudRate = 9600;
        private int stopBits = 1;
        private int dataBits = 8;
        private int parity = 0;
        private int flowCon = 0;
        private int flags = 0;
        private int timeOut = 5;
        private SerialPortProtocolCodecFactory mCodecFactory;
        private SerialPortHandler mSerialPortHandler;

        public Builder setSerialPortNumber(String serialPortNumber) {

            this.serialPortNumber = serialPortNumber;
            return this;
        }

        public Builder setBaudRate(int baudRate) {

            this.baudRate = baudRate;
            return this;
        }

        public Builder setStopBits(int stopBits) {

            this.stopBits = stopBits;
            return this;
        }

        public Builder setDataBits(int dataBits) {

            this.dataBits = dataBits;
            return this;
        }

        public Builder setParity(int parity) {

            this.parity = parity;
            return this;
        }

        public Builder setFlowCon(int flowCon) {

            this.flowCon = flowCon;
            return this;
        }

        public Builder setFlags(int flags) {

            this.flags = flags;
            return this;
        }

        public Builder setTimeOut(int timeOut) {

            this.timeOut = timeOut;
            return this;
        }

        public Builder setSerialPortProtocolCodecFactory(SerialPortProtocolCodecFactory codecFactory) {

            this.mCodecFactory = codecFactory;
            return this;
        }

        public Builder setSerialPortHandler(SerialPortHandler serialPortHandler) {

            this.mSerialPortHandler = serialPortHandler;
            return this;
        }

        private void applyConfig(SerialPortConfig config) {

            config.serialPortNumber = this.serialPortNumber;
            config.baudRate = this.baudRate;
            config.stopBits = this.stopBits;
            config.dataBits = this.dataBits;
            config.parity = this.parity;
            config.flowCon = this.flowCon;
            config.flags = this.flags;
            config.timeOut = this.timeOut;
            config.codecFactory = this.mCodecFactory;
            config.serialPortHandler = this.mSerialPortHandler;
        }

        public SerialPortConfig builder() {

            SerialPortConfig serialPortConfig = new SerialPortConfig();
            applyConfig(serialPortConfig);

            return serialPortConfig;
        }
    }
}
